<?
/**
 * @package Base
 * @subpackage Controller
 */
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is the standard Views manager for the framework<br>
 *
 * Views are html files, that can (and mostly will) include variables to be shown. The view is parsed as a PHP file, so you can use PHP syntax to show your variables, and also you can use PHP code to control cycles, such as foreach, while, etc.
 * 
 * It has only one internal command:
 *
 * <code>
 * {include file="file_to_be_included" dir="path_to_file"}
 * </code>
 *
 * "file_to_be_included" <b>MUST</b> have its extension.
 * "path_to_file" <b>MUST</b> be a valid path 
 * E.g.: {include file="header.php" dir="
 *
 * @author Daniel Marjos <dmarjos@gmail.com>
 * @version 1.0
 */
class Base_Controller_View {

	/**
	 * @ignore
	 */
	public $_viewVars=array();
	/**
	 * @ignore
	 */
	public $template_dir="";
	
	/**
	 * @ignore
	 */
	function __construct() {
	}
	
	/**
	 * Use this method to assign a value to a view's variable
	 * @param string $var The variable to be assigned
	 * @param mixed $val The value
	 */
	function assign($var,$val) {
		$this->_viewVars[$var]=$val;
	}

	/**
	 * Use this method to get the value for a view's variable
	 * @param string $var The variable to be retrieved
	 */
	function get($var) {
		return $this->_viewVars[$var];
	}

	/**
	 * @ignore
	 */
	function __ParseParameters($theParamStr) {

		$params=array();
		if (preg_match_all('/([A-Za-z_0-9]*)="([A-Za-z0-9_\s\+\-\*\/\.\(\)\[\]=><!\'&|]*)"/s',$theParamStr,$matches,PREG_SET_ORDER)) {
			foreach($matches as $match) {
				$theParamValue=$match[2];
				if (substr($theParamValue,-1)=="/") $theParamValue=substr($theParamValue,0,-1);
				$params[strtoupper($match[1])]=str_replace('"','',$theParamValue);
			}
		} elseif (preg_match_all('/([A-Za-z_0-9]*)=\'([A-Za-z0-9_\+\-\*\/\.\(\)\[\]=><!]*)\'/s',$theParamStr,$matches,PREG_SET_ORDER)) {
			foreach($matches as $match) {
				$theParamValue=$match[2];
				if (substr($theParamValue,-1)=="/") $theParamValue=substr($theParamValue,0,-1);
				$params[strtoupper($match[1])]=str_replace('"','',$theParamValue);
			}
		}
		return $params;
	}

	/**
	 * @ignore
	 */
	function processTags(&$content) {
		if (preg_match_all("'{(INCLUDE)(\s+)([^}]*?)([\s]*)}'si",$content,$matches,PREG_SET_ORDER)) {
			foreach($matches as $match) {
				$tplCommand=$match[1];
				$tplCmdParams=$this->__ParseParameters($match[3]);
//				dump_var($tplCmdParams);
				if($tplCmdParams["FILE"]) {
					$path=$tplCmdParams["FILE"];
					if ($tplCmdParams["DIR"]) {
						$dir=$tplCmdParams["DIR"];
						if (substr($dir,-1)!="/") $dir.="/";
						$path=$_SERVER["DOCUMENT_ROOT"]."/".$dir.$path;
					} else {
						$dir=$this->template_dir;
						if (substr($dir,-1)!="/") $dir.="/";
						$path=$dir.$path;
					}
					$content=str_replace($match[0],"<?php include(\"$path\");?>",$content);
				}
			}
		}
	}
	
	/**
	 * This method fetchs and returns the processed view.
	 * 
	 * @param string $path the path where the view file resides.
	 * @return string the parsed html to be sent to the browser
	 */
	function fetch($path) {
		extract($this->_viewVars);
		if (!file_exists($path)) {
			$this->_fetchError($path);
		}
		$viewFile=file_get_contents($path);
		$this->processTags($viewFile);
		ob_start();
		eval("?>$viewFile<?");
		$view=ob_get_contents();
		ob_end_clean();
		return $view;
	}

	function _fetchError($path) {
		Base_Controller_View_Error::Show("View doesn't exists","The file $path is missing");
	}
}